package it.carlom.stickyheader.example.fragment;


import it.carlom.stickyheader.ResourceTable;
import it.carlom.stickyheader.example.Utils;
import it.carlom.stickyheader.example.adapter.TextAdapter;
import it.carlom.stikkyheader.core.StikkyHeaderBuilder;
import it.carlom.stikkyheader.core.animator.AnimatorBuilder;
import it.carlom.stikkyheader.core.animator.BaseStickyHeaderAnimator;
import it.carlom.stikkyheader.core.animator.HeaderStikkyAnimator;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.utils.Point;
import ohos.agp.utils.Rect;

public class ActionBarImageFragment extends BaseFraction {

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        return scatter.parse(ResourceTable.Layout_fragment_actionbarimage, container, false);
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        ListContainer listView = (ListContainer) getComponent().findComponentById(ResourceTable.Id_listview);
        Component header = getComponent().findComponentById(ResourceTable.Id_header);
        final Component title = getComponent().findComponentById(ResourceTable.Id_title);
        listView.setItemProvider(new TextAdapter(getFractionAbility(), Utils.getData()));


        BaseStickyHeaderAnimator animator = new HeaderStikkyAnimator() {

            @Override
            public AnimatorBuilder getAnimatorBuilder() {
                Component viewToAnimate = getHeader().findComponentById(ResourceTable.Id_header_image);
                final Component titleToolbar = getComponent().findComponentById(ResourceTable.Id_title_toolbar);
                final Rect squareSizeToolbar = new Rect(0, 0, title.getHeight(), title.getHeight());

                return AnimatorBuilder.create()
                        .applyScale(viewToAnimate, squareSizeToolbar)
                        .applyTranslation(viewToAnimate, new Point(titleToolbar.getRight(), 0))
                        .applyFade(viewToAnimate, 1f);
            }
        };

        StikkyHeaderBuilder.stickTo(listView).setHeader(header)
                .minHeightHeader(250)
                .animator(animator)
                .build();
    }


}
